import sun.invoke.empty.Empty;

import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-10-28
 * Time:21:27
 */
public class MySingleList {
    //一个节点代表一个对象，所以用内部内实现
    static class ListNode{
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }
    public static ListNode head;//整个链表的头节点，所以定义在整个链表里面

    public void creatList(){
        ListNode node1=new ListNode(13);
        ListNode node2=new ListNode(34);
        ListNode node3=new ListNode(45);
        ListNode node4=new ListNode(47);
        ListNode node5=new ListNode(56);
        ListNode node6=new ListNode(67);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=node6;

        this.head=node1;
    }
    public void display() {
        ListNode cur=head;
        while (cur!=null){//cur==null证明把链表走完了
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
    //头插法
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        node.next=head;
        head=node;

    }
    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        //找尾
        ListNode cur=head;
        if(cur==null){
            head=node;
            return;
        }
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //判断位置是否合法
        if(index<0||index>size()){
            System.out.println("位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        ListNode cur=findIndexSubOne(index);
        ListNode node=new ListNode(data);
        node.next=cur.next;
        cur.next=node;
    }
    //找要插入节点位置的前一个节点
    private ListNode findIndexSubOne(int index){
        ListNode cur=head;
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head==null){
            return;
        }
        //单独删除头结点
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=searchPrev(key);
        if (cur==null){
            System.out.println("没有你要删除的节点");
            return;
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }
    private ListNode searchPrev(int key){
        ListNode cur=head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else {
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count=0;
        ListNode cur=head;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    public void clear() {
        this.head=null;
    }

    public ListNode FindKthToTail(int k) {
        if(k<=0||head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        for (int i = 0; i < k-1; i++) {
            fast=fast.next;
            if (fast==null){
                return null;
            }
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
//创造环
    public void creatLoop(){
        ListNode cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=head.next;
    }
    //判断是否有环
    public boolean hasCycle() {
        if(head==null){
            return false;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }


    //删除链表中重复的结点
    public ListNode deleteDuplication(ListNode pHead){
        ListNode cur=pHead;
        ListNode newHead=new ListNode(-1);
        ListNode tmpHead=newHead;
        //遍历链表的每个结点
        while (cur!=null){
            if(cur.next!=null&&cur.val==cur.next.val){
                while(cur.next!=null&&cur.val==cur.next.val){
                    cur=cur.next;
                }
                cur=cur.next;
            }else {
                tmpHead.next=cur;
                tmpHead=tmpHead.next;
                cur=cur.next;
            }
        }
        return tmpHead.next;
    }
    //逆序打印链表
    public void reversePrintList() {
        Stack<ListNode> stack=new Stack<>();
        ListNode cur=head;
        while (cur!=null){
            stack.push(cur);
            cur=cur.next;
        }
        System.out.println();
        while (!stack.isEmpty()) {
            ListNode top=stack.pop();
            System.out.print(top.val+" ");
        }
        System.out.println();


    }
}

